<?php
namespace app\admin\controller;
//use app\admin\controller\Common;
use \org\Upload;
use think\Config;
use think\Env;
use think\Request;
use think\Controller;
use think\Db;
use app\admin\model\AdminModel;
use app\index\model\UserModel;
use app\admin\model\NewsModel;
use app\admin\model\UploadModel;
class Admin extends Controller
{	/*frame框架界面*/
	//请求frame
	public function frame()
	{
		return $this->fetch('admin/frame/frame');
	}
	//请求frame界面Index
	public function index()
	{
		return $this->fetch('admin/frame/index');
	}
	//请求frame界面top
	public function top()
	{
		return $this->fetch('admin/frame/top');
	}
	//请求frame界面pages/index
	public function pages()
	{
		return $this->fetch('admin/frame/pages/index');
	}
/*	//请求frame界面admin_add
	public function pagesadmin_adds(){
		return $this->fetch('admin/pagesadmin_add');
	}*/
	public function pagesadmin_add()
	{
		$data=input('post.');
		$admin=new  AdminModel($data);
		$res=$admin->allowField(true)->save();
		if ($res) {
			$this->success('添加成功','admin/pagesadmin_list');
		}else{
			$this->error('添加失败');
		}
	}
	//请求frame界面admin_list
	public function pagesadmin_list()
	{
		$data=AdminModel::paginate(3);
		$page=$data->render();
		$this->assign('data',$data);
		$this->assign('page',$page);
		return $this->fetch('admin/frame/pages/admin_list');
	}
	//请求frame界面admin_update
	public function pagesadmin_update()
	{
		$id=input('get.id');
		$data=AdminModel::get($id);
		$this->assign('data',$data);
		return $this->fetch('admin/frame/pages/admin_update');
	}

//修改功能pagesadmin_edit
	public function pagesadmin_edit(){
		$data=input('post.');
		$id=input('post.id');
		$admin=new AdminModel();
		$res=$admin->allowField(true)->save($data,['id'=>$id]);
		if ($res) {
			$this->success('成功修改信息','admin/pagesadmin_list');
		}else{
			$this->error('错误');
		}
	}

	//删除功能pagesadmin_delete
	public function pagesadmin_delete(){
		$id=input('get.id');
		$res=AdminModel::destroy($id,true);
		if ($res) {
			$this->success('删除信息成功','admin/pagesadmin_list');
		}else{
			$this->error('错误');
		}
	}
	
	//请求frame界面user_list
	public function pagesuser_list()
	{
		$data=UserModel::paginate(3);
		$page=$data->render();
		$this->assign('data',$data);
		$this->assign('page',$page);
		return $this->fetch('admin/frame/pages/user_list');
	}
	//请求frame界面news_list
	public function pagesnews_list()
	{
		$data=NewsModel::paginate(3);
		$page=$data->render();
		$this->assign('data',$data);
		$this->assign('page',$page);
		return $this->fetch('admin/frame/pages/news_list');
	}
	
	//请求frame界面news_list
	public function pagesnews_add()
	{
		$data=input('post.');
		$news=new  NewsModel($data);
		$res=$news->allowField(true)->save();
		if ($res) {
			$this->success('添加成功','admin/pagesnews_list');
		}else{
			$this->error('添加失败');
		}
	}
	//请求frame界面news_update
	public function pagesnews_update()
	{
		$id=input('get.id');
		$data=NewsModel::get($id);
		$this->assign('data',$data);
		return $this->fetch('admin/frame/pages/news_update');
	}

	//修改功能news_edit
	public function pagesnews_edit(){
		$data=input('post.');
		$id=input('post.id');
		$news=new NewsModel();
		$res=$news->allowField(true)->save($data,['id'=>$id]);
		if ($res) {
			$this->success('成功修改信息','admin/pagesnews_list');
		}else{
			$this->error('错误');
		}
	}

	//删除功能news_delete
	public function pagesnews_delete(){
		$id=input('get.id');
		$res=NewsModel::destroy($id,true);
		if ($res) {
			$this->success('删除信息成功','admin/pagesnews_list');
		}else{
			$this->error('错误');
		}
	}

	//查找功能news_select
	public function pagesnews_select(){
		$id=input('get.id');
		$data=NewsModel::get($id);
		$this->assign('data',$data);
		dump($data);

		die;
		//$res=newsModel::destroy($id,true);
		if ($res) {
			$this->success('删除信息成功','admin/pagesnews_list');
		}else{
			$this->error('错误');
		}
	}

	public function pagesnews_selecttitle(){
		$title=input('get.title');

		$data=NewsModel::get($title);
		$this->assign('data',$data);
		dump($data);

		die;
		//$res=newsModel::destroy($id,true);
		if ($res) {
			$this->success('删除信息成功','admin/pagesnews_list');
		}else{
			$this->error('错误');
		}
	}



//请求frame界面pic_list
	public function pagespic_list()
	{
		$data=UploadModel::paginate(3);
		$page=$data->render();
		$this->assign('data',$data);
		$this->assign('page',$page);
		return $this->fetch('admin/frame/pages/pic_list');
	}

	public function pagespic_upload(){
	 //	print_r($_FILES);
		$data=input('post.');
		$file = request()->file('picurl');
		
    // 移动到框架应用根目录/public/uploads/ 目录下
		if($file){
			$info = $file->move(ROOT_PATH . 'public/static/' . DS . 'uploads');
			$picurl=$info->getSaveName();
			dump($picurl);
			die;
			$upload=new UploadModel($data,$picurl);
			// dump($upload);
			// die;
			$res=$upload->allowField(true)->save();
			if ($res) {
				$this->success('添加成功','admin/pagespic_list');
			}else{
				$this->error('添加失败');
			}
			if($info){
            // 成功上传后 获取上传信息
            // 输出 jpg
				echo $info->getExtension()."<br/>";
            // 输出整体路径
				echo $info->getSaveName()."<br/>";
            // 输出文件路径
				echo $info->getFilename()."<br/>"; 
			}else{
            // 上传失败获取错误信息
				echo $file->getError();
			}


		}
	}

	//admin首页

	//请求登录界面
	public function login(){
		return $this->fetch('login/login');
	}
	//用户添加
	public function user_add(){
		$data=input('post.');
		$admin=new  AdminModel($data);
		$res=$admin->allowField(true)->save();
		if ($res) {
			$this->success('添加成功','index/user_list');
		}else{
			$this->error('添加失败');
		}
	}
	//用户列表
	public function user_list(){
		$data=UserModel::paginate(3);
		$page=$data->render();
		$this->assign('data',$data);
		$this->assign('page',$page);
		return $this->fetch('index/user_list');

	}

	//admin登录功能页面
	public function checklogin(Request $request){

		$data=input('post.');
		$admin=new AdminModel($data);

		$res=$admin->where('name',$data['name'])->find();
		if ($res) {
			if ($res['password']===$data['password']) {
				$value=session('name',$data['name']);

			}else{
				$this->error('密码不正确');
			}
		}else{
			$this->error('用户名不存在');
			exit;
		}
		if (captcha_check($data['code'])) {

			$this->success('验证码正确，登录成功','index/index');
		}else{
			$this->error('验证码不正确');

		}
	}
	//管理员注销功能logout
	public function logout(){
		session(null);
		$this->success('退出成功',"index/index/index");
	}
	//添加管理员功能admin_add
	public function admin_add()
	{
		$data=input('post.');
		$admin=new  AdminModel($data);
		$result=$admin->where('name',$data['name'])->find();
		if ($result ==null) {
			$res=$admin->allowField(true)->save();
			if ($res) {
				$this->success('添加成功','admin/admin_list');
			}else{
				$this->error('添加失败');
			}
		}else{
			$this->error('添加失败,用户名已经存在');
		}

		// $res=$admin->allowField(true)->save();
		// if ($res) {
		// 	$this->success('添加成功','index/index');
		// }else{
		// 	$this->error('添加失败');
		// }
	}
	//显示列表功能admin_list
	public function admin_list()
	{
		$data=AdminModel::paginate(3);
		$page=$data->render();
		$this->assign('data',$data);
		$this->assign('page',$page);
		return $this->fetch('index/admin_list');
	}
	//查找需要修改的id
	public function admin_update()
	{
		$id=input('get.id');
		$data=AdminModel::get($id);
		$this->assign('data',$data);
		return $this->fetch('index/admin_update');
	}
	//修改功能admin_edit
	public function admin_edit(){
		$data=input('post.');
		$id=input('post.id');
		$admin=new AdminModel();
		$res=$admin->allowField(true)->save($data,['id'=>$id]);
		if ($res) {
			$this->success('成功修改信息','admin/admin_list');
		}else{
			$this->error('错误');
		}
	}

	//删除功能admin_delete
	public function admin_delete(){
		$id=input('get.id');
		$res=AdminModel::destroy($id,true);
		if ($res) {
			$this->success('删除信息成功','admin/admin_list');
		}else{
			$this->error('错误');
		}
	}


}
